Frame Animation 就是把很多張圖片串起來變成動畫。
步驟:
1.設定動畫的圖片集合
2.在Activity啟動動畫。
在res/drawable,新增frame_animation.xml
裡的每個item代表一張圖片。待會我們就會將這些圖片串成一個動畫。
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/selected"
    android:oneshot="false">
    <item
        android:drawable="@drawable/circle"
        android:duration="500" />
    <item
        android:drawable="@drawable/circle2"
        android:duration="500" />
    <item
        android:drawable="@drawable/circle3"
        android:duration="500" />
    <item
        android:drawable="@drawable/circle4"
        android:duration="500" />
    <item
        android:drawable="@drawable/circle5"
        android:duration="500" />
    <item
        android:drawable="@drawable/circle6"
        android:duration="500" />
</animation-list>
步驟1:將圖片背景轉為AnimationDrawable
步驟2:AnimationDrawable.start(),開始動畫
imageView.setBackgroundResource(R.drawable.frame_animation)
start.setOnClickListener {
    //步驟1:將圖片背景轉為AnimationDrawable
    val frameAnimation = imageView.background as AnimationDrawable
    //步驟2:AnimationDrawable.start(),開始動畫
    frameAnimation.start()
}
stop.setOnClickListener {
    val frameAnimation = imageView.background as AnimationDrawable
    //結束動畫
    frameAnimation.stop()
}
Frame Animation 是一個比較簡單的動畫。實際上逐格動畫只會用在較小的動畫效果,如果複雜的動畫或是圖片數較多的就不建議了。載入太多的圖片也可能會造成OOM。
完整程式:https://github.com/evanchen76/FrameAnimation